package cn.elane.kibana.demo.global.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.UUID;

/**
 * @author Chenyu
 */
public class StringUtil {

	/**
	 * 获取36位UUID字符串
	 *
	 * @return uuid
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString();
	}

	/**
	 * 获取32位大写UUID
	 *
	 * @return UUID
	 */
	public static String getGUID() {
		return UUID.randomUUID().toString().toUpperCase().replace("-", "");
	}

	/**
	 * 获取32位UUID
	 *
	 * @return UUID
	 */
	public static String getGUID32() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	/**
	 * 根据Unicode编码完美的判断中文汉字和符号
	 */
	private static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION;
	}

	/**
	 * 判断是否符合json对象格式.
	 *
	 * @param str p
	 * @return r
	 */
	public static Boolean ifJSONObjectStr(String str) {
		if (str == null || str.trim().length() == 0) {
			return false;
		}
		str = str.trim();
		try {
			if (str.startsWith("{") && str.endsWith("}")) {
				JSONObject jo = JSONObject.parseObject(str);
				return jo != null && !jo.isEmpty();
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 判断是否符合json对象数组格式.
	 *
	 * @param str p
	 * @return r
	 */
	public static Boolean ifJSONArrayStr(String str) {
		if ((str == null || str.trim().length() == 0)) {
			return false;
		}
		str = str.trim();
		try {
			if (str.startsWith("[{") && str.endsWith("}]")) {
				JSONArray ja = JSONArray.parseArray(str);
				return ja != null && !ja.isEmpty();
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	public static void main(String[] args) {
		System.out.println(getUUID());
		System.out.println(getGUID());
		System.out.println(UUID.randomUUID().toString());
		System.out.println(UUID.randomUUID().toString().toUpperCase());
	}

}
